import logging
from logging.handlers import TimedRotatingFileHandler
import os
from app import islocaltion,start_scheduler
from app import app as mainapp

app = mainapp
if not islocaltion:
    # 确保log目录存在
    if not os.path.exists('log'):
        os.makedirs('log')
    
    # 配置按日期轮转的日志处理器
    log_handler = TimedRotatingFileHandler(
        filename="./log/log.log",  # 初始文件名
        when="midnight",           # 每天午夜轮转
        interval=1,                # 轮转间隔为1天
        backupCount=7             # 保留X天的日志
    )
    
    # 设置日志文件名格式为 log-YYYY-MM-DD.log
    log_handler.suffix = "%Y-%m-%d.log"
    log_handler.setLevel(logging.INFO)
    
    # 创建日志格式
    formatter = logging.Formatter(
        '%(asctime)s %(levelname)s %(name)s %(message)s'
    )
    log_handler.setFormatter(formatter)
    
    # 配置根日志记录器
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    logger.addHandler(log_handler)

start_scheduler()